//
//  TVLinkingCardViewController.swift
//  iPhone
//
//  Created by Toj on 8/28/22.
//

import UIKit

private let linkButtonHeight: CGFloat = 32

// MARK: - 懒加载(属性), 变量, 初始化init
class TVLinkingCardViewController: STBaseViewController {
    
   // public var activityType: ActivityH5Type = .none
   
    public var smartCardID: String {
        set {
            cardIDTextField.text = newValue
        }
        
        get {
            return cardIDTextField.text
        }
    }
    
    public var decoderNumber: String {
        set {
            cardCODETextField.text = newValue
        }
        get {
            return cardCODETextField.text
        }
    }
    
    private lazy var tipsLabel: UILabel = {
        let style = NSMutableParagraphStyle()
        style.lineBreakMode = .byTruncatingTail
        style.alignment = .left
        let font = UIFont.systemFont(ofSize: 14.f_scale)
        let text = "Link your StarTimes decoder account to enjoy online self-service and watch VIP channels for free in App. Learn more here."
        let highText = text.replacingOccurrences(of: ".", with: "").split(separator: Character.init(" ")).last ?? ""
        
        let (attributedText, _) = text.to_attributedString(font: font, color: "#999999".to_color, paragraph: style, highlight: String(highText), hcolor: "#0087EB".to_color, paragraph: style, hlineStyle: .underline)
            
        let view = UILabel()
        view.numberOfLines = 0
        view.attributedText = attributedText
        return view
    }()
    
    private lazy var cardIDTextField: FMTextField = {
        let view = FMTextField()
        
        view.maxCharCount = 11
        
        view.titleLabel.text = "Smart Card ID"
        view.titleLabel.textColor = "#AAAAAA".to_color
        view.titleLabel.font = UIFont.systemFont(ofSize: 12)
        
        // view.textField.delegate = self
        view.textField.borderStyle = .none
        view.textField.clearButtonMode = .whileEditing // 清空按钮
        view.textField.font = UIFont.systemFont(ofSize: 18)
        view.textField.keyboardType = .numberPad
        view.textField.textColor = "#333333".to_color
        let attributedPlaceholder = "Use 11 digits for Smart Card ID".to_attributedString(font:  UIFont.systemFont(ofSize: 15), color: "#AAAAAA".to_color)
        view.textField.attributedPlaceholder = attributedPlaceholder
        
        view.maxCharLabel.textColor = "#999999".to_color
        view.maxCharLabel.font = UIFont.systemFont(ofSize: 12)
        
        let rightView = UIButton()
        rightView.setImage("commodity_tv_card_question".to_image, for: .normal)
        rightView.addTarget(self, action: #selector(didTapCardQuestion), for: .touchUpInside)
        rightView.frame.size = CGSize(width: 24, height: 24)
        
        view.rightView = rightView
        return view
    }()
    
    private lazy var cardCODETextField: FMTextField = {
        let view = FMTextField()
        view.maxCharCount = 6
        view.charFormatType = .threeFourFour
        
        view.titleLabel.text = "Decoder S/N"
        view.titleLabel.textColor = "#AAAAAA".to_color
        view.titleLabel.font = UIFont.systemFont(ofSize: 12)
        
        view.textField.delegate = self
        view.textField.borderStyle = .none
        view.textField.keyboardType = .asciiCapable
        view.textField.clearButtonMode = .whileEditing // 清空按钮
        view.textField.font = UIFont.systemFont(ofSize: 18)
        view.textField.textColor = "#333333".to_color
        let attributedPlaceholder = "The last 6 digits of your decoder S/N".to_attributedString(font:  UIFont.systemFont(ofSize: 15), color: "#AAAAAA".to_color)
        view.textField.attributedPlaceholder = attributedPlaceholder
        
        view.maxCharLabel.textColor = "#999999".to_color
        view.maxCharLabel.font = UIFont.systemFont(ofSize: 12)
        
        let rightView = UIButton()
        rightView.setImage("commodity_tv_card_question".to_image, for: .normal)
        rightView.addTarget(self, action: #selector(didTapCODEQuestion), for: .touchUpInside)
        rightView.frame.size = CGSize(width: 24, height: 24)
        
        view.rightView = rightView
        return view
    }()
   
    private lazy var linkButton: UIButton = {
       let view = UIButton()
        view.setTitle("LINK", for: .normal)
        view.setTitleColor("#775E33".to_color, for: .normal)
        view.setTitleColor("#A3A3A3".to_color, for: .disabled)
        view.backgroundColor = "#E6E6E6".to_color
        view.cm_cornerRadius = linkButtonHeight * 0.5
        return view
    }()
}

// MARK: - 生命周期
extension TVLinkingCardViewController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
        setupMakeInitialize()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        view.endEditing(true)
    }
}

// MARK: - 数据请求
extension TVLinkingCardViewController {
    
}

// MARK: - Public
extension TVLinkingCardViewController {
    
}

// MARK: - Private
extension TVLinkingCardViewController {
    
    private func reloadTextFields() {
        let cardID = cardIDTextField.text
        let cardCODE = cardCODETextField.text
        var colors = ["#E6E6E6".to_color.cgColor, "#D6D6D6".to_color.cgColor]
        let isEnabled = !cardID.isEmpty && !cardCODE.isEmpty
        if isEnabled {
            colors = ["#F5DB9D".to_color.cgColor, "#E8CD8C".to_color.cgColor]
        }

        linkButton.isEnabled = isEnabled
        linkButton.gd_add(colors: colors)
    }
}

// MARK: - 点击事件
extension TVLinkingCardViewController {
    
    @objc private func didTapCardQuestion() {
        
    }
    
    @objc private func didTapCODEQuestion() {
        
    }
}

// MARK: - 代理事件
extension TVLinkingCardViewController: UITextFieldDelegate {
    
    func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
        if textField != cardIDTextField.textField { return true }
        
        let lenth = string.lengthOfBytes(using: .utf8)
        for lindex in 0..<lenth {
            let char = (string as NSString).character(at: lindex)
            if char < 48 || char > 57 {
                return false
            }
        }
        
        return true
    }
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        view.endEditing(true)
        return true
    }
    
    func textFieldDidEndEditing(_ textField: UITextField, reason: UITextField.DidEndEditingReason) {
        reloadTextFields()
    }
}

// MARK: - 代理事件
extension TVLinkingCardViewController {
    
}

// MARK: - Private
extension TVLinkingCardViewController {
    
}

// MARK: - 初始化
extension TVLinkingCardViewController {
    
    private func setupMakeAddSubviews() {
        view.addSubview(tipsLabel)
        view.addSubview(cardIDTextField)
        view.addSubview(cardCODETextField)
        view.addSubview(linkButton)
    }
    
    private func setupMakeLayoutSubviews() {
        tipsLabel.snp.makeConstraints { make in
            make.left.top.equalTo(16)
            make.right.equalTo(view.snp.right).offset(-16)
        }
        
        cardIDTextField.snp.makeConstraints { make in
            make.top.equalTo(112)
            make.left.right.equalTo(tipsLabel)
            make.height.equalTo(80)
        }
        
        cardCODETextField.snp.makeConstraints { make in
            make.top.equalTo(cardIDTextField.snp.bottom).offset(24)
            make.left.right.equalTo(tipsLabel)
            make.height.equalTo(cardIDTextField)
        }
        
        linkButton.snp.makeConstraints { make in
            make.top.equalTo(cardCODETextField.snp.bottom).offset(88)
            make.left.right.equalTo(cardCODETextField)
            make.height.equalTo(linkButtonHeight)
        }
    }
    
    private func setupMakeInitialize() {
        view.backgroundColor = .white
        
        view.layoutIfNeeded()
        
        reloadTextFields()
    }
}
